//
//  DeferredViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/24.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxSwift

class DeferredViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addTapWithHandle { (tap) in
        
            let disBag = DisposeBag()
            
            
            // 只有当有订阅者订阅的时候才会去创建序列。
            let deferredObs = Observable<Int>.deferred({ () -> Observable<Int> in
                
                print("creating...")
                
                return Observable<Int>.create({ (obs) -> Disposable in
                    obs.onNext(1)
                    obs.onNext(2)
                    obs.onNext(3)
                    obs.onCompleted()
                    return Disposables.create()
                })
            })
            
            deferredObs.subscribe({ (eve) in
                print(eve)
            }).disposed(by: disBag)
        
        }
        
    }
}
